package com.test.util;

import java.util.HashSet;
import java.util.Set;
import com.test.dao.QuestionDao;
import com.test.table.Answer;
import com.test.table.Question;
import com.test.table.TrueAnswer;

public class Saver {

	private Factory factory = Factory.getInstance();
	private QuestionDao questionDao = factory.getQuestionDao();
	
	public void save(String question, String kind, Set<String> answer, Set<String> trueAnswer){
		
		Question q = new Question();
		q.setKind(kind);
		q.setQuestion(question);
		
		Set<Answer>     answers     = new HashSet<>();
		Set<TrueAnswer> trueAnswers = new HashSet<>();
		
		for(String s: answer){
			Answer ans = new Answer();
			ans.setAnswer(s);
			ans.setIdQuestion(q);
			answers.add(ans);
		}
		
		for(String s: trueAnswer){
			TrueAnswer t = new TrueAnswer();
			t.setTrueAnswer(s);
			t.setId_Question(q);
			trueAnswers.add(t);
		}
		
		q.setAnswer(answers);
		q.setTrueAnswer(trueAnswers);
		
		try {
			questionDao.addQuestion(q);
		} catch (Exception e) {
			e.printStackTrace();
		}	
	}
}
